package com.sky.setmeal.controller;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.setmeal.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 套餐管理 controller
 */
@Slf4j
@Api(tags = {"套餐管理"}, description = "后台套餐操作")
@Validated
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {


    @Resource
    private SetMealService setMealService;



    /**
     * 套餐管理
     *        新增套餐
     */
    @ApiOperation(tags = {"套餐管理"}, value = "新增套餐", notes = "数据库新增套餐信息")
    @PostMapping
    public Result addSetMeal(@RequestBody @Validated SetmealDTO setmealDTO){
        log.info("套餐管理 - 新增套餐: {}", setmealDTO);
        setMealService.addSetMealAndSetMealDish(setmealDTO);
        return Result.success();
    }



    /**
     * 套餐管理
     *        分页条件查询
     */
    @ApiOperation(tags = {"套餐管理"}, value = "套餐分页条件查询", notes = "套餐分页-条件查询列表")
    @GetMapping("/page")
    public Result pageQuery(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐管理 - 套餐分页-条件查询: {}", setmealPageQueryDTO);
        PageResult pageResult = setMealService.selectPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }



    /**
     * 套餐管理
     *       根据ids 批量删除套餐 以及 套餐菜品关联数据
     */
    @ApiOperation(tags = {"套餐管理"}, value = "删除套餐", notes = "删除套餐及套餐菜品关联")
    @DeleteMapping
    public Result deleteSetmealAndSetmealDish(@NotEmpty(message = "id为空") @RequestParam("ids") List<Long> ids){
        log.info("套餐管理 - 根据ids批量删除套餐: {}", ids);
        setMealService.deleteSetmealAndSetmealDish(ids);
        return Result.success();
    }



    /**
     * 套餐管理
     *       根据id查询套餐 以及 套餐菜品关联
     */
    @ApiOperation(tags = {"套餐管理"}, value = "查询套餐及菜品关联", notes = "根据id查询套餐以及套餐菜品关联")
    @GetMapping("/{id}")
    public Result selectSetmealAndSetmealDish(@PathVariable("id")Long id){
        log.info("套餐管理 - 根据id: {} 查询套餐及套餐菜品关联信息", id);
        SetmealVO setmealVO = setMealService.selectSetmealAndSetmealDish(id);
        return Result.success(setmealVO);
    }



    /**
     * 套餐管理
     *        修改套餐
     */
    @ApiOperation(tags = {"套餐管理"}, value = "修改套餐及关联", notes = "修改套餐及关联菜品")
    @PutMapping
    public Result updateSetmealAndSetmealDish(@RequestBody @Validated SetmealDTO setmealDTO){
        log.info("套餐管理 - 修改套餐以及套餐菜品关联数据: {}", setmealDTO);
        setMealService.updateSetmealAndSetmealDish(setmealDTO);
        return Result.success();
    }



    /**
     * 套餐管理
     *        套餐启用、禁用
     */
    @ApiOperation(tags = {"套餐管理"}, value = "套餐启用、禁用", notes = "套餐启用、禁用修改")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable(name="status")Integer status, @NotNull(message = "id为空")Long id){
        log.info("套餐管理 - 套餐id: {},当前操作(1启用/0禁用) ==> {}", id, status);
        setMealService.updateStatusById(id, status);
        return Result.success();
    }








}
